from selenium.webdriver import Keys,ActionChains
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# 启动浏览器
driver=webdriver.Edge()
# 窗口最大化
driver.maximize_window()
driver.get('https://www.baidu.com/Index.htm')
# 点击贴吧 显示等待
a=WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,'//a[@href="http://tieba.baidu.com/"]')))
a.click()
# 切换页面
driver.switch_to.window(driver.window_handles[-1])
# 搜索黑神话
driver.find_element(By.CLASS_NAME,'search_ipt search_inp_border j_search_input tb_header_search_input').send_keys('黑神话')
driver.find_element(By.XPATH,'//*[@id="thread_top_list"]/li[1]/div/div[2]/div/div[1]/a').click()
# 切换页面
driver.switch_to.window(driver.window_handles[-1])
# 点击置顶评论
driver.find_element(By.LINK_TEXT,'参与黑神话二创抽精美周边和门票').click()
# xpath高阶定位
driver.find_element(By.XPATH,'//a[starts-with(@class,".j_tbnav_tab_a")]')
# 收藏
driver.find_element(By.CLASS_NAME,'btn-sub.btn-small.j_favor')
time.sleep(3)
# 键盘事件 粘贴
a=driver.find_element(By.CLASS_NAME,'search_internal_input j_search_internal_input')
a.send_keys('悟空')
a.send_keys(Keys.CONTROL,'c')
b=driver.find_element(By.CLASS_NAME,'search_internal_input j_search_internal_input')
b.send_keys(Keys.CONTROL,'v')
